//app.js
import encrypt from "./utils/md5.js";
App({

    // domain: 'http://39.108.88.197/',
    // domain: 'http://zhuoyu.com/',
    domain: 'https://zy.dlongt.com/',
    globalData: {
        remote_conf: null, //服务器配置,
        api_key: '1nMgfd2#$weKfeKxf#2086',
        school_id: '',
        school_name: '',
        role: '',
        user: '',
    },
    onLaunch: function () {
            const updateManager = wx.getUpdateManager()

            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                // console.log(res.hasUpdate)
            })

            updateManager.onUpdateReady(function () {
                wx.showModal({
                    title: '更新提示',
                    content: '新版本已经准备好，是否重启应用？',
                    success: function (res) {
                        if (res.confirm) {
                            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                            updateManager.applyUpdate()
                        }
                    }
                })
            })

            updateManager.onUpdateFailed(function () {
                // 新版本下载失败
            })

            // wx.request({
            //     url: this.domain + '/common/common/init',
            //     header: {
            //         'content-type': 'application/json'
            //     },
            //     method: 'get',
            //     success: (res) => {
            //         let data = res.data.data;
            //         data.diff_time = (new Date).getTime() - data.service_time * 1000; //前后端时间差
            //         this.globalData.remote_conf = data;
            //     },
            //     fail: (res) => {
            //         this.showWarn('网络开小差去了！');
            //     }
            // });
        }
        /**requist请求 */
        ,
    request: function (method, url, data, successes, fails, completes) {
            wx.showNavigationBarLoading();
            let App = this,
                //   time = parseInt(this.globalData.remote_conf.diff_time) + parseInt((new Date).getTime()),
                time = '',
                token = wx.getStorageSync('token'),
                sign = encrypt.md5(this.globalData.api_key + time + token);
            wx.request({
                url: App.domain + url,
                header: {
                    sign: sign,
                    time: time,
                    'content-type': method === 'get' ? 'application/json' : 'application/x-www-form-urlencoded',
                    token: token //保持会话
                },
                method: method,
                data: data,
                success: function (res) {
                    //res.data.data && res.data.data.token && wx.setStorageSync('token', res.data.data.token);
                    //定义：如果后端状态码返回420，则需要去登录
                    if (res.data.code === 420) {
                        //todo login
                        let login_url = 'pages/inside/inside';
                        if (App.curPage().route !== login_url) {
                            wx.switchTab({
                                url: '/' + login_url, //跳转到登录页面

                            });
                        }
                        fails && fails(res);
                        return false;
                    } else if (res.data.code === 400) {
                        App.showError(res.data.msg, function () {
                            fails && fails(res);
                        });
                        return false;
                    } else {
                        successes && successes(res);
                    }

                },
                fail: function (res) {
                    App.showWarn('网络开小差去了！', function () {
                        fails && fails(res);
                    });
                },
                complete: function (res) {
                    // wx.hideLoading();
                    wx.hideNavigationBarLoading();
                    completes && completes(res);
                }
            });
        }

        ,
    _post: function (url, data, success, fail, complete) {
        return this.request('post', url, data, success, fail, complete)
    },
    _get: function (url, data, success, fail, complete) {
        return this.request('get', url, data, success, fail, complete)
    },
    showError: function (msg, callback) {
        wx.showToast({
            title: msg,
            // image: '/images/warning.png',
            icon: 'none',
            success: function () {
                callback && (setTimeout(function () {
                    callback();
                }, 1500));
            }
        });
    },
    showSuccess: function (msg, callback) {
        wx.showToast({
            title: msg,
            image: '/images/success.png',
            success: function () {
                callback && (setTimeout(function () {
                    callback();
                }, 1500));
            }
        });
    },
    showWarn(msg, callback) {
        wx.showToast({
            title: msg,
            image: '/images/warning.png',
            success: function () {
                callback && (setTimeout(function () {
                    callback();
                }, 1500));
            }
        });
    },
    showTips: function (msg, callback) {
        wx.showToast({
            title: msg,
            icon: 'none',
            success() {
                callback && callback()
            }
        })
    },
    curPage() {
        let pages = getCurrentPages()
        return pages[pages.length - 1]
    },

    beLogin(callback) {
        this.curPage().selectComponent("#login").show(callback);
    }
})